package com.bling.common.base.dao;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.bling.common.page.PageResult;

/**
 * 通用DAO接口
 * @author BLingSoft
 * 主要是增删改查，搜索采用lucene搜索引擎
 * 
 * 其他各个操作均继承这个接口，其他接口如果在其他方法可以自行扩展
 */
public class BaseDao<T> implements IBaseDao<T> {

	public void save(T t) throws SQLException {
		Session session = sessionFactory.getCurrentSession();
		session.save(t);
	}

	public void delete(T t) throws SQLException {
		Session session = sessionFactory.getCurrentSession();
		session.delete(t);
	}

	public void delete(Class<T> clazz, Serializable id) throws SQLException {
		Session session = sessionFactory.getCurrentSession();
		session.delete(session.load(clazz, id));
	}

	public void delete(String hql) throws SQLException {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery(hql);
		query.executeUpdate();
	}

	public void delete(String hql, Object[] params) throws SQLException {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery(hql);
		int size = params.length;
		for (int i = 0; i < size; i++)
			query.setParameter(i, params[i]);
		query.executeUpdate();
	}

	@SuppressWarnings("unchecked")
	public T find(Class<T> clazz, Serializable id) throws SQLException {
		Session session = sessionFactory.getCurrentSession();
		return (T) session.get(clazz, id);
	}

	@SuppressWarnings("unchecked")
	public T find(String hql) throws SQLException {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery(hql);
		List<T> list = query.list();
		if (null != list && list.size() > 0)
			return list.get(0);
		return null;
	}

	@SuppressWarnings("unchecked")
	public T find(String hql, Object[] params) throws SQLException {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery(hql);
		int size = params.length;
		for (int i = 0; i < size; i++)
			query.setParameter(i, params[i]);
		List<T> list = query.list();
		if (null != list && list.size() > 0)
			return list.get(0);
		return null;
	}

	@SuppressWarnings("unchecked")
	public int getCount(String hql) throws SQLException {
		/**
		 * 因为查询条数select count(*) from table 即使没有结果仍然返回0
		 */
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery(hql);
		List<Integer> list = query.list();
		if (null != list && list.size() > 0)
			return (Integer) list.get(0);
		return 0;
	}

	@SuppressWarnings("unchecked")
	public int getCount(String hql, Object[] params) throws SQLException {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery(hql);
		int size = params.length;
		for (int i = 0; i < size; i++)
			query.setParameter(i, params[i]);
		List<Integer> list = query.list();
		if (null != list && list.size() > 0)
			return (Integer) list.get(0);
		return 0;
	}

	@SuppressWarnings("unchecked")
	public PageResult<T> pageList(final String hql, final int from,
			final int num) throws SQLException {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery(hql);
		query.setFirstResult(from);
		query.setMaxResults(num);
		List<T> list = query.list();
		return new PageResult<T>(getCount(hql), list);
	}

	@SuppressWarnings("unchecked")
	public PageResult<T> pageList(final String hql, final Object[] params,
			final int from, final int num) throws SQLException {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery(hql);
		query.setFirstResult(from);
		query.setMaxResults(num);
		int size = params.length;
		for (int i = 0; i < size; i++)
			query.setParameter(i, params[i]);
		List<T> list = query.list();
		return new PageResult<T>(getCount(hql, params), list);
	}

	public void update(T t) throws SQLException {
		Session session = sessionFactory.getCurrentSession();
		session.update(t);
	}

	public void update(String hql) throws SQLException {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery(hql);
		query.executeUpdate();
	}

	public void update(String hql, Object[] params) throws SQLException {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery(hql);
		int size = params.length;
		for (int i = 0; i < size; i++)
			query.setParameter(i, params[i]);
		query.executeUpdate();
	}

	@SuppressWarnings("unchecked")
	public PageResult<T> listAll(String hql) throws SQLException {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery(hql);
		List<T> list = query.list();
		return new PageResult<T>(list.size(), list);
	}

	@SuppressWarnings("unchecked")
	public PageResult<T> listAll(String hql, Object[] params)
			throws SQLException {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery(hql);
		int size = params.length;
		for (int i = 0; i < size; i++)
			query.setParameter(i, params[i]);
		List<T> list = query.list();
		return new PageResult<T>(list.size(), list);
	}

	@Resource
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
}
